/*
 * MyUnitTesterGUI
 * 
 * Version information:1.0
 *
 * Date: 19/11/2012
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author dv11rlm
 */
public class MyUnitTesterGUI extends JPanel {

    private static final long serialVersionUID = 1L;
    private JTextArea textArea;
    private JButton clearTextButton;
    private JButton runButton;
    private JTextField runInput;

    /**
     * Setup GUI.
     */
    public MyUnitTesterGUI() {
	setLayout(new BorderLayout());
	setFocusable(true);

	// input text setup
	JPanel runInputPane = inputTextSetup();

	// output text setup
	JScrollPane areaScrollPane = outputTextSetup();

	// textArea text clearing button
	JPanel clearTextButtonPanel = textClearingButton();

	// runButton setup
	JPanel runButtonPanel = runButtonSetup();

	// Put topPane together.
	topPaneLayoutSetup(runInputPane, runButtonPanel);

	// Put middlePane together.
	middlePaneLayoutSetup(areaScrollPane);

	// Put bottomPane together.
	bottomPaneLayoutSetup(clearTextButtonPanel);

    }

    /**
     * Setup runButton.
     * 
     * @return JPanel
     */
    private JPanel runButtonSetup() {
	runButton = new JButton("Run");
	JPanel runButtonPanel = new JPanel();
	runButtonPanel.add(runButton);
	runButton.addActionListener(new RunButtonListener(runInput, textArea));
	return runButtonPanel;
    }

    /**
     * Setup clearTextButton.
     * 
     * @return JPanel
     */
    private JPanel textClearingButton() {
	clearTextButton = new JButton("Clear");
	JPanel clearTextButtonPanel = new JPanel();
	clearTextButtonPanel.add(clearTextButton);
	clearTextButton.addActionListener(new ClearButtonListener(textArea));
	return clearTextButtonPanel;
    }

    /**
     * Setup JScrollscrollPane and JTextArea and add the JTextArea to
     * the JScrollPane.
     * 
     * @return JScrollPane
     */
    private JScrollPane outputTextSetup() {
	textArea = new JTextArea();
	textArea.setFont(new Font("Serif", Font.CENTER_BASELINE, 14));
	textArea.setLineWrap(true);
	textArea.setWrapStyleWord(true);
	textArea.setEditable(false);
	textArea.addKeyListener(new KeyboardListener(runInput, textArea));

	JScrollPane areaScrollPane = new JScrollPane(textArea);
	areaScrollPane
		.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	areaScrollPane.setPreferredSize(new Dimension(600, 500));
	return areaScrollPane;
    }

    /**
     * Setup inputField.
     * 
     * @return JPanel
     */
    private JPanel inputTextSetup() {
	runInput = new JTextField(46);
	JPanel runInputPane = new JPanel();
	runInputPane.add(runInput);
	return runInputPane;
    }

    /**
     * adding bottomPane to JPane. Adding clear button to it
     * 
     * @return JPanel
     */
    private void bottomPaneLayoutSetup(JPanel clearTextButtonPanel) {
	JPanel bottomPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
	bottomPane.add(clearTextButtonPanel);
	add(bottomPane, BorderLayout.SOUTH);
    }

    /**
     * Adding middlePane to JPane.
     * 
     * @return JPanel
     */
    private void middlePaneLayoutSetup(JScrollPane areaScrollPane) {
	JPanel middlePane = new JPanel(new BorderLayout());
	middlePane.add(areaScrollPane);
	add(middlePane, BorderLayout.LINE_START);
    }

    /**
     * Adding topPane to JPane. Adding Runbutton and InputField
     * 
     * @return JPanel
     */
    private void topPaneLayoutSetup(JPanel runInputPane, JPanel runButtonPanel) {
	JPanel topPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
	topPane.add(runButtonPanel);
	topPane.add(runInputPane);
	add(topPane, BorderLayout.NORTH);
    }
}
